@echo off
set IMAGE_NAME=mytodolist
set CONTAINER_NAME=mytodolist
set TEMP_IMAGE_NAME=%IMAGE_NAME%-temp

echo --- Step 1: Building new temporary image: %TEMP_IMAGE_NAME% ---
docker build -t %TEMP_IMAGE_NAME% .
if %errorlevel% neq 0 (
    echo Docker build failed!
    pause
    exit /b 1
)

echo.
echo --- Step 2: Stopping and removing existing container: %CONTAINER_NAME% ---
docker stop %CONTAINER_NAME%
docker rm %CONTAINER_NAME%

echo.
echo --- Step 3: Removing old image: %IMAGE_NAME% ---
docker rmi %IMAGE_NAME%

echo.
echo --- Step 4: Tagging temporary image to final name: %IMAGE_NAME% ---
docker tag %TEMP_IMAGE_NAME% %IMAGE_NAME%
docker rmi %TEMP_IMAGE_NAME%

echo.
echo --- Step 5: Running new container: %CONTAINER_NAME% ---
docker run -d --name %CONTAINER_NAME% -p 7626:5173 -v /d/docker/mytodolist/data:/app/data %IMAGE_NAME%

echo.
echo --- Redeployment complete! ---
pause